<?php

namespace app\api\controller;

class Checkin
{
    public $pagesize = 5;


    /**
     * 获取用户一个课程中需要的打卡数据
     */
    public function getCheckinList()
    {
        $auth = trim(input("request.auth"));//用户auth
        $class_id = trim(input("request.class_id"));
        $userInfo = checkLoginExp($auth);
        $currDate = getCurrDate();
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($class_id)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[class_id]'];
            return json($return);
        }
        //获取课程信息
        $classlistInfo = getClasslistInfo($class_id);
        if (empty($classlistInfo)) {
            $return = ['err' => 3, 'msg' => '挑战信息不存在', 'devMsg' => '获取失败，参数错误[class_id]'];
            return json($return);
        }

        //判断当前用户是否加入了该课程
        if (isJoinInClass($userInfo['id'], $class_id) == 0) {
            $return = ['err' => 1, 'msg' => '操作失败,您未加入该课程', 'devMsg' => '操作失败，参数错误[class_id]'];
            return json($return);
        }

        $list = getCheckinList($userInfo['id'], $class_id);

        foreach ($list as $key => $val) {
            $list[$key]['isCurrentDay'] = 0;
            $tempDate = $val['y'] . "-" . $val['m'] . "-" . $val['d'];
            if ($tempDate == $currDate['simple']) {
                $list[$key]['isCurrentDay'] = 1;
            }
        }

        $return = ['err' => 0, 'msg' => '获取成功', 'data' => $list];
        $return['startClickinTime'] = date("Y-m-d", strtotime($classlistInfo['task_time']));//开始打卡时间
        $return['totleClickinNum'] = $classlistInfo['days'];//课程总天数
        //查询总的完成天数
        $chickInModel = new  \app\admin\model\ClassCheckin();
        $Complatelist = $chickInModel->getDataForWheres(array('uid' => $userInfo['id'], 'classid' => $classlistInfo['id'], 'type' => 1), "id");
        $return['totleComplateNum'] = 0;//总完成天数
        if ($Complatelist) {
            $return['totleComplateNum'] = count($Complatelist);//总完成天数
        }
        $return['currToday'] = $currDate['simple'];
        return json($return);
    }


    /**
     * @param auth 用户数据
     * @param class_id 课程ID
     * @return \think\response\Json
     */
    public function sign()
    {
        $auth = trim(input("request.auth"));//用户auth
        $class_id = trim(input("request.class_id"));
        $userInfo = checkLoginExp($auth);
        if (empty($userInfo)) {
            $return = ['err' => 1, 'msg' => '操作失败,未登录', 'devMsg' => '操作失败，参数错误[auth]'];
            return json($return);
        }
        if (empty($class_id)) {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败，参数错误[class_id]'];
            return json($return);
        }
        if (!isJoinInClass($userInfo['id'], $class_id)) {
            $return = ['err' => 1, 'msg' => '操作失败,您未加入该挑战', 'devMsg' => '操作失败，参数错误[class_id]'];
            return json($return);
        }
        $currDate = getCurrDate();
        $classInfo = getClasslistInfo($class_id);

        //获取今日打卡记录数据
        $where = [
            'classid' => $class_id,
            'uid' => $userInfo['id'],
            'y' => $currDate['year'],
            'm' => $currDate['month'],
            'd' => $currDate['day'],
        ];
        $classCheckinModel = new \app\admin\model\ClassCheckin();
        $classCheckinInfo = $classCheckinModel->getDataForWhere($where);
        if (empty($classCheckinInfo)) {
            $return = ['err' => 1, 'msg' => '今日无需打卡', 'devMsg' => '操作失败，今日无需打卡，未查询到需打卡记录'];
            return json($return);
        }

        if ($classCheckinInfo['type'] == 1) {
            $return = ['err' => 1, 'msg' => '今日已经打卡，请不要重复操作', 'devMsg' => '今日已经打卡，请不要重复操作'];
            return json($return);
        }

        if (!empty($classCheckinInfo['click_start']) && !empty($classCheckinInfo['click_end'])) {
            $currHour = date("H:i");
            if ($classCheckinInfo['click_start'] > $currHour) {
                $return = ['err' => 1, 'msg' => '还未到打卡时间', 'devMsg' => '还未到打卡时间'];
                return json($return);
            }
            if ($classCheckinInfo['click_end'] < $currHour) {
                $return = ['err' => 1, 'msg' => '已经错过了打卡时间', 'devMsg' => '已经错过了打卡时间'];
                return json($return);
            }
        }
        $res = userSign($userInfo, $classCheckinInfo);
        if ($res) {
            //判断是否是 最后一天 如果是最后一天需要，需要判断是否完成任务
            if ($classInfo['days'] == $classCheckinInfo['day']) {
                $is_win = 0;
                if (checkCheckinStatus($userInfo['id'], $class_id)) {
                    $is_win = 1;
                } else {
                    $is_win = 2;
                }
                $classUserjoin = new \app\admin\model\ClassUserjoin();
                $classUserjoin->updateDataForWhere(array('is_win' => $is_win), array('uid' => $userInfo['id'], 'classid' => $class_id));
            }
            $return = ['err' => 0, 'msg' => '操作成功', 'devMsg' => '操作成功'];
            return json($return);
        } else {
            $return = ['err' => 1, 'msg' => '操作失败', 'devMsg' => '操作失败'];
            return json($return);
        }

    }
}